package com.dong.com.love.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dong.com.love.Bean.NewsPic;
import com.dong.com.love.api.UrlConfig;
import com.dong.com.love.base.BaseListAdapter;
import com.dong.com.love.listener.CardItemClickListener;
import com.dong.com.mvvmdemo.R;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 首页ListView适配器
 * Created by Dong on 2016/10/31.
 */

public class NewsAdapter extends BaseListAdapter {

    private CardItemClickListener cardItemClickListener;
    private ViewHolder holder;

    public NewsAdapter(Context context, List<NewsPic.TngouBean> list) {
        this.context = context;
        this.list = list;
    }
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        ViewHolder viewHolder= null;
        if(view ==null){
            viewHolder=new ViewHolder();
            view=View.inflate(context, R.layout.item_mainlv,null);


            viewHolder.tv=(TextView)view.findViewById(R.id.item_news_tv);
            viewHolder.img=(ImageView) view.findViewById(R.id.item_news_img);
            view.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder) view.getTag();
        }
        NewsPic.TngouBean bean= (NewsPic.TngouBean) list.get(i);
        String title = bean.getTitle();
        viewHolder.tv.setText(title);
        String imgurl= UrlConfig.BASE_IMG_URL+bean.getImg().trim();
        if(imgurl!=null&&!"".equals(imgurl)){
            Picasso.with(context)
                    .load(imgurl)
                    .placeholder(R.drawable.title)
                    .error(R.drawable.title)
                    .into(viewHolder.img);
        }
        holder=viewHolder;
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                cardItemClickListener.onCardClickListener(i);
            }
        });
        return view;
    }

    /*
      设置Item的点击监听
     */
    public void setCardItemClickListener(CardItemClickListener cardItemClickListener){
        this.cardItemClickListener=cardItemClickListener;
    }

    public class ViewHolder{
        TextView tv;
        ImageView img;
    }
}
